package org.hxzon.blogbackup.baidu;

import java.io.IOException;

import org.hxzon.util.DebugUtil;
import org.hxzon.util.DownloadUrl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ImageBackuper {

    public static void backupPageImage(int index) {
        String origUrl = null;
        try {
            Document doc = Jsoup.parse(BlogWriter.readImageInfo(index));
            Element body = doc.body();
            Elements items = body.getElementsByClass("item");
            for (Element item : items) {
                String category = item.getElementsByClass("category").first().text();
                String filepath = item.getElementsByClass("file").first().text();
                filepath = BlogWriter.itemDirectory + category + "/" + filepath;
                origUrl = item.getElementsByClass("orig").first().text();
                DownloadUrl.saveToFile(origUrl, filepath);
            }
        } catch (Exception e) {
            DebugUtil.error("fetch item err," + index + ",url=" + origUrl + ",:" + e.getMessage(), e);
        }
    }

    public static void main(String args[]) throws IOException {
        //255-251
        //250-241
        //240-201
        //200-181,180-151
        //150-101,100-51,50-0
        DebugUtil.clearDebugClass();
        DebugUtil.addDebugClass(ImageBackuper.class.getCanonicalName());
//        for (int i = 242; i >= 242; i--) {
//            backupPageImage(i);
//            DebugUtil.info(i + ",ok");
//        }
    }
}
